function getName() {
<<<<<<< HEAD

}
getName(); //run the function
=======
    var user = JSON.parse(localStorage.getItem("loggedinUser"));
    if (user) {
        console.log("Currently logged in:  " + user["name"]);
        document.getElementById("name-goes-here").innerText = user["name"];
    } else {
        console.log("No one is logged in");
    }
}
getName(); //run the function

function addCupListener() {
    document.getElementById("addCupButton").addEventListener("click", function () {
        // find out who is logged in
        var user = JSON.parse(localStorage.getItem("loggedinUser"));
        if (user) {
            //increment the counter
            user["count"]++;
            //save into loggedinUser
            localStorage.setItem("loggedinUser", JSON.stringify(user));
            // display it
            document.getElementById("coffee-count-goes-here").innerText = user["count"];
            //get the users list, and update the count for THAT user
            updateUsersCount(user["email"], user["count"])
        } else {
            console.log("No one is logged in");
        }
    });
}
addCupListener();

function updateUsersCount(email, countValue) {
    var users = JSON.parse(localStorage.getItem("users"));
    if (users) {
        const person = users.find(u => u.email === email);
        if (person) {
            person.count = countValue;
            addToHistory(person);
        }
        localStorage.setItem("users", JSON.stringify(users));
    }
}

function addToHistory(person) {
    const now = new Date();
    const formatted = now.toLocaleString('en-US', {
        weekday: 'long', // "Monday", "Tuesday", etc.
        year: 'numeric', // "2021", "2022", etc.
        month: 'long', // "January", "February", etc.
        day: 'numeric', // 1, 2, 3, etc.
        hour: 'numeric', // 12 AM, 1 PM, etc.
        minute: 'numeric',
        second: 'numeric'
    });

    if (person.history === null) //if there are no users yet
        person.history = []; //initialize it to empty
    person.history.push({ date: formatted });
}

// 添加 getCount 函数
function getCount() {
    var who = JSON.parse(localStorage.getItem("loggedinUser"));
    if (who) {
        document.getElementById("coffee-count-goes-here").innerText = who["count"];
        console.log("Current coffee count: " + who["count"]);
    } else {
        console.log("No one is logged in");
    }
}
getCount();

// 添加 addPlusMinusListener 函数
function addPlusMinusListener() {
    document.addEventListener("DOMContentLoaded", function () {
        // 获取按钮和数字输入框
        const minusBtn = document.getElementById('minus-btn');
        const plusBtn = document.getElementById('plus-btn');
        const input = document.getElementById('number-input');

        // 如果 minus (-) 按钮被点击，获取输入框的值，如果大于零则减 1
        minusBtn.addEventListener('click', function () {
            const currentValue = Number(input.value) || 0;
            if (currentValue > 0) {
                input.value = currentValue - 1;
            }
        });

        // 如果 plus (+) 按钮被点击，输入框的值加 1
        plusBtn.addEventListener('click', function () {
            const currentValue = Number(input.value) || 0;
            input.value = currentValue + 1;
        });
    });
}
addPlusMinusListener();

// 添加 addCupsListener 函数
function addCupsListener() {
    document.getElementById("addCupsButton").addEventListener("click", function () {
        var number = document.getElementById("number-input").value;
        // 检查输入是否为有效的数字
        if (isNaN(Number(number))) {
            alert("Please enter a valid number.");
            return;
        }
        // 找出当前登录用户
        var user = JSON.parse(localStorage.getItem("loggedinUser"));
        if (user) {
            // 增加计数器并保存到 loggedinUser
            user["count"] = Number(user["count"]) + Number(number);
            localStorage.setItem("loggedinUser", JSON.stringify(user));

            // 更新用户列表中的用户计数
            updateUsersCount(user["email"], user["count"]);

            // 显示更新后的计数
            document.getElementById("coffee-count-goes-here").innerText = user["count"];

            // 重置输入框的值
            document.getElementById("number-input").value = 0;
        } else {
            console.log("No one is logged in");
        }
    });
}
addCupsListener();
>>>>>>> history
